package com.kanshu.ksgb.fastread.doudou.module.book.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.baseadapter.CustomFragmentAdapter;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.view.tablayout.SlidingTabLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.fragment.IChangeSexListener;
import com.kanshu.ksgb.fastread.doudou.module.book.fragment.RankingListFragment;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 排行榜
 * Created by wcy on 2017\7\11 0011.
 */
@Route(path = ARouterConfig.RANKING_LIST)
public class RankingListActivity extends BaseActivity {

    public static final String EXTRA_TAB_POS = "tab_pos";
    public static final String EXTRA_SITE = "site";
    @BindView(R.id.tab_layout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mPager;
    int mSite = Constants.MALE;
    ImageView mImageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranklist_layout);
        ButterKnife.bind(this);
        setTitle("排行榜");
        mImageView = new ImageView(this);
        mTitle.getRightContainer().addView(mImageView);
        int tabPos = getIntent().getIntExtra(EXTRA_TAB_POS, 0);
        mSite = getIntent().getIntExtra(EXTRA_SITE, Constants.MALE);
        mImageView.setImageResource(mSite == Constants.MALE ? R.mipmap.ic_switch_male : R.mipmap.ic_switch_female);
        final List<Fragment> list = new ArrayList<>();
        list.add(RankingListFragment.newInstance(RankingListFragment.TYPE_RANKINGLIST_RENQI, mSite));
        list.add(RankingListFragment.newInstance(RankingListFragment.TYPE_RANKINGLIST_COLLECT, mSite));
        list.add(RankingListFragment.newInstance(RankingListFragment.TYPE_RANKINGLIST_FINISH, mSite));
        String[] strs = new String[]{"人气榜", "收藏榜", "完本榜"};
        CustomFragmentAdapter pagerAdapter = new CustomFragmentAdapter(getSupportFragmentManager(), list);
        mPager.setAdapter(pagerAdapter);
        mTabLayout.setViewPager(mPager, strs);
        mTabLayout.setCurrentTab(tabPos);
        mPager.setOffscreenPageLimit(strs.length);
        mImageView.setOnClickListener(v -> {
            if (mSite == Constants.MALE) {
                mImageView.setImageResource(R.mipmap.ic_switch_female);
                mSite = Constants.FEMALE;
            } else {
                mImageView.setImageResource(R.mipmap.ic_switch_male);
                mSite = Constants.MALE;
            }
            for (Fragment fragment : list) {
                if (fragment.isAdded() && fragment instanceof IChangeSexListener) {
                    ((IChangeSexListener) fragment).onChangeSex(mSite);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }
}
